package com.aouth2.test.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author cxsj
 * @title: OAUTH2登录退出  暂时屏蔽使用时开启即可
 * @date 2020-07-24 7:57
 */
@RestController
public class OauthController {

    private static final Logger logger = LoggerFactory.getLogger(OauthController.class);
    @Autowired
    private OAuth2AuthorizedClientService authorizedClientService;


    /**
     * @Description 登录页面
     * @Param OAuth2AuthenticationToken
     * @Return ResultDTO
     * @Author cxsj
     * @Date 2020/08/21
     */
    @GetMapping("/welcome")
    public String welcome(OAuth2AuthenticationToken authentication, HttpServletRequest request){
        OAuth2AuthorizedClient authorizedClient =
                this.authorizedClientService.loadAuthorizedClient(
                        authentication.getAuthorizedClientRegistrationId(),
                        authentication.getName());

        OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
        logger.info("获取TOKEN{}",accessToken.getTokenValue());
        logger.info("获取PrincipalName{}",authorizedClient.getPrincipalName());
        logger.info("获取用户信息{}",authentication.getPrincipal());

        return "欢迎登录";
    }


    /**
     * @Description 自定义注销
     * @Return ResultDTO
     * @Author cxsj
     * @Date 2020/08/21
     */
    @GetMapping("/xx/logout")
    public String logout(){

        logger.info("logout---------------------已经注销");
        return "logout";
    }


    /**
     * @Description 重定向获取CODE
     * @Return ResultDTO
     * @Author 于洪波
     * @Date 2020/08/21
     */
    @GetMapping(value="/login/oauth2/code/welcome", produces = "application/json;charset=utf-8")
    public String welcome(Model model) {
        return "欢迎光临";
    }

}